﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FinModel
{
    public class Bond
    {
        /// <summary>
        /// 面值
        /// </summary>
        public double Face { get; set; }
        /// <summary>
        /// 折扣
        /// </summary>
        public double Discount { get; set; }
        /// <summary>
        /// 售价
        /// </summary>
        public double Price { get; set; }
        /// <summary>
        /// 利息
        /// </summary>
        public double Coupon { get; set; }
        /// <summary>
        /// 付利息的频率，如半年一次，则CouponN = 0.5
        /// </summary>
        public double CouponN { get; set; }
        /// <summary>
        /// 起息日
        /// </summary>
        public DateTime StartDate { get; set; }
        /// <summary>
        /// 到期日
        /// </summary>
        public DateTime MatuirtyDate { get; set; }
        /// <summary>
        /// 采用连续复利模型进行定价
        /// </summary>
        /// <param name="yieldCurve"></param>
        /// <returns></returns>
        public double PriceIt(YieldCurve yieldCurve)
        {
            int n = (int)Math.Truncate((MatuirtyDate - StartDate).TotalDays / 360 / CouponN);
            double couponFace = Face * Coupon;
            double result = 0f;
            for (int i = 1; i < n; ++i)
            {
                result += couponFace * Rate.ContCR(yieldCurve[i * CouponN], i);
            }
            result += (Face + couponFace) * Rate.ContCR(yieldCurve[n * CouponN], n);

            return result;
        }
    }
}
